package org.jrad.util;

import java.io.File;

import javax.xml.XMLConstants;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;

import org.apache.commons.digester.Digester;
import org.jrad.model.Attribute;
import org.jrad.model.EntityCrud;
import org.jrad.model.PersistenceUnitData;
import org.jrad.model.Project;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

public class ParserXML {

	public final static Project parserXML(String xml, String xsd) throws Exception{
		Project project = null;
		Digester digester = new Digester();
		Schema schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(new File(xsd));
		digester.setXMLSchema(schema);
		digester.setValidating(true);
		digester.setFeature("http://apache.org/xml/features/validation/schema", true);
		digester.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true);
		digester.addObjectCreate("project", Project.class);
		digester.setErrorHandler(new ErrorHandler() {
			
			@Override
			public void warning(SAXParseException arg0) throws SAXException {
				System.out.println("Warning");
			}
			
			@Override
			public void fatalError(SAXParseException arg0) throws SAXException {
				System.out.println("fatalError");
			}
			
			@Override
			public void error(SAXParseException arg0) throws SAXException {
				System.out.println("Error");
			}
		});
		
		digester.addBeanPropertySetter("project/name", "name");
		digester.addBeanPropertySetter("project/packageRoot", "packageRoot");
		digester.addObjectCreate("project/persistenceUnitData", PersistenceUnitData.class);
		digester.addBeanPropertySetter("project/persistenceUnitData/name", "name");
		digester.addBeanPropertySetter("project/persistenceUnitData/provider", "provider");
		digester.addBeanPropertySetter("project/persistenceUnitData/url", "url");
		digester.addBeanPropertySetter("project/persistenceUnitData/user", "user");
		digester.addBeanPropertySetter("project/persistenceUnitData/password", "password");
		digester.addBeanPropertySetter("project/persistenceUnitData/driver", "driver");
		digester.addSetNext("project/persistenceUnitData", "setPersistenceUnitData");
		digester.addObjectCreate("project/cruds/EntityCrud", EntityCrud.class);
		digester.addBeanPropertySetter("project/cruds/EntityCrud/name", "name");
		digester.addObjectCreate("project/cruds/EntityCrud/attributes/attribute", Attribute.class);
		digester.addBeanPropertySetter("project/cruds/EntityCrud/attributes/attribute/name", "name");
		digester.addBeanPropertySetter("project/cruds/EntityCrud/attributes/attribute/type", "type");
		digester.addBeanPropertySetter("project/cruds/EntityCrud/attributes/attribute/size", "size");
		digester.addBeanPropertySetter("project/cruds/EntityCrud/attributes/attribute/validation", "validation");
		digester.addBeanPropertySetter("project/cruds/EntityCrud/attributes/attribute/required", "required");
		digester.addSetNext("project/cruds/EntityCrud/attributes/attribute", "addAttribute");
		digester.addSetNext("project/cruds/EntityCrud", "addCrud");
		try {
			project = (Project) digester.parse(new File(xml));	
		}
		catch (Exception e) {
			System.out.println("ERRO DE PARSER");
		}
		
		return project;
	}
}
